﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace RoleHelper
{
    class Personaje: NPC
    {
        private int Experiencia;
        private string jugador;

        public Personaje()
        {
        }
        public Personaje(string jug, string name, int vMax, int pMax, int lvl, int exp, bool mask, char sex, int bo, int bd)
        {
            jugador = jug;
            setNombre(name);
            setVidaMax(vMax);
            setVidaAct(vMax);
            setppMax(pMax);
            setppAct(pMax);
            setNivel(lvl);
            Experiencia = exp;
            setMasked(mask);
            setSexo(sex);
            setBO(bo);
            setBD(bd);
        }
        public void setExp(int exp)
        {
            Experiencia = exp;
        }
        public int getExp()
        {
            return Experiencia;
        }
        public void setJugador(string jug)
        {
            jugador = jug;
        }
        public string getJugador()
        {
            return jugador;
        }
        public new bool guardar(string nombre_fich)
        {
            try
            {
                string filename;

                filename = nombre_fich;
                
                XmlTextWriter save = new XmlTextWriter(filename, null);
                save.Formatting = Formatting.Indented;
                save.WriteStartDocument();
                save.WriteStartElement("player");
                save.WriteAttributeString("id", Convert.ToString((getID())));
                save.WriteAttributeString("jugador", jugador);
                save.WriteElementString("nombre", getNombre());
                save.WriteElementString("vida", Convert.ToString(getVidaAct()));
                save.WriteElementString("vidamax", Convert.ToString(getVidaMax()));
                save.WriteElementString("pp", Convert.ToString(getppAct()));
                save.WriteElementString("ppmax", Convert.ToString(getppMax()));
                save.WriteElementString("nivel", Convert.ToString(getNivel()));
                save.WriteElementString("experiencia", Convert.ToString(getExp()));
                save.WriteElementString("sexo", Convert.ToString(getSexo()));
                if (getMasked() == true)
                {
                    save.WriteElementString("sexomasked", "true");
                }
                else
                {
                    save.WriteElementString("sexomasked", "false");
                }
                save.WriteElementString("BO", Convert.ToString(getBO()));
                if (getBomasked() == true)
                {
                    save.WriteElementString("bomasked", "true");
                }
                else
                {
                    save.WriteElementString("bomasked", "false");
                }
                save.WriteElementString("BD", Convert.ToString(getBD()));
                if (getBdmasjed() == true)
                {
                    save.WriteElementString("bdmasked", "true");
                }
                else
                {
                    save.WriteElementString("bdmasked", "false");
                }
                save.WriteElementString("estado", Convert.ToString(getestado()));
                save.WriteElementString("turnos", Convert.ToString(getturnos()));
                if (gethabilidades() != null)
                {
                    List<habilidad> hab = gethabilidades();
                    for (int i = 0; i < hab.Count; i++)
                    {
                        save.WriteStartElement("habilidad");
                        save.WriteAttributeString("atr1", hab[i].getatributo1());
                        save.WriteAttributeString("atr2", hab[i].getatributo2());
                        save.WriteAttributeString("coste1", Convert.ToString(hab[i].getcoste1()));
                        save.WriteAttributeString("coste2", Convert.ToString(hab[i].getcoste2()));
                        save.WriteAttributeString("coste3", Convert.ToString(hab[i].getcoste3()));
                        save.WriteElementString("nombre", hab[i].getnombre());
                        save.WriteElementString("descripcion",hab[i].getdescripcion());
                        save.WriteElementString("rango",Convert.ToString(hab[i].getrango()));
                        save.WriteEndElement();
                    }
                }
                if (getlistahechizos() != null)
                {
                    List<listahechizos> lhech = getlistahechizos();
                    for (int i = 0; i < lhech.Count; i++)
                    {
                        save.WriteStartElement("lista_hechizos");
                        save.WriteAttributeString("nivel", Convert.ToString(lhech[i].getnivel()));
                        save.WriteAttributeString("reino", lhech[i].getreino());
                        List<hechizo> hech = lhech[i].gethechizos();
                        for (int j = 0; j < hech.Count; j++)
                        {
                            save.WriteStartElement("hechizo");
                            save.WriteAttributeString("nivel", Convert.ToString(hech[j].getlvl()));
                            save.WriteElementString("nombre", hech[j].getnombre());
                            save.WriteElementString("descripcion", hech[j].getdesc());
                            save.WriteElementString("daño", Convert.ToString(hech[j].getdaño()));
                            save.WriteElementString("critico", Convert.ToString(hech[j].getcritico()));
                            save.WriteElementString("tipo_critico", hech[j].gettipocritico());
                            save.WriteEndElement();
                        }
                        save.WriteEndElement();
                    }
                }
                save.WriteEndElement();
                save.WriteEndDocument();

                save.Flush();
                save.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public new bool cargar(string ruta)
        {
            try
            {
                XmlTextReader nuevo = new XmlTextReader(ruta);
                while (nuevo.Read())
                {
                    switch (nuevo.LocalName)
                    {
                        case "player":
                            if(nuevo.NodeType == XmlNodeType.Element)
                                jugador = nuevo.GetAttribute("jugador");
                            break;
                        case "nombre":
                            if (nuevo.NodeType == XmlNodeType.Element)
                                this.setNombre(nuevo.ReadElementString());
                            break;
                        case "vida":
                            if (nuevo.NodeType == XmlNodeType.Element)
                                this.setVidaAct(Convert.ToInt32(nuevo.ReadElementString()));
                            break;
                        case "vidamax":
                            if (nuevo.NodeType == XmlNodeType.Element)
                                this.setVidaMax(Convert.ToInt32(nuevo.ReadElementString()));
                            break;
                        case "pp":
                            if (nuevo.NodeType == XmlNodeType.Element)
                                this.setppAct(Convert.ToInt32(nuevo.ReadElementString()));
                            break;
                        case "ppmax":
                            if (nuevo.NodeType == XmlNodeType.Element)
                                this.setppMax(Convert.ToInt32(nuevo.ReadElementString()));
                            break;
                        case "nivel":
                            if (nuevo.NodeType == XmlNodeType.Element)
                                this.setNivel(Convert.ToInt32(nuevo.ReadElementString()));
                            break;
                        case "experiencia":
                            if (nuevo.NodeType == XmlNodeType.Element)
                                this.setExp(Convert.ToInt32(nuevo.ReadElementString()));
                            break;
                        case "sexo":
                            if (nuevo.NodeType == XmlNodeType.Element)
                                this.setSexo(Convert.ToChar(nuevo.ReadElementString()));
                            break;
                        case "sexomasked":
                            if (nuevo.NodeType == XmlNodeType.Element)
                                this.setMasked(Convert.ToBoolean(nuevo.ReadElementString()));
                            break;
                        case "BO":
                            if (nuevo.NodeType == XmlNodeType.Element)
                                this.setBO(Convert.ToInt32(nuevo.ReadElementString()));
                            break;
                        case "bomasked":
                            if (nuevo.NodeType == XmlNodeType.Element)
                                this.setBomasked(Convert.ToBoolean(nuevo.ReadElementString()));
                            break;
                        case "BD":
                            if (nuevo.NodeType == XmlNodeType.Element)
                                this.setBD(Convert.ToInt32(nuevo.ReadElementString()));
                            break;
                        case "bdmasked":
                            if (nuevo.NodeType == XmlNodeType.Element)
                                this.setBdmadked(Convert.ToBoolean(nuevo.ReadElementString()));
                            break;
                        case "estado":
                            if (nuevo.NodeType == XmlNodeType.Element)
                                this.setestado(Convert.ToChar(nuevo.ReadElementString()));
                            break;
                        case "turnos":
                            if (nuevo.NodeType == XmlNodeType.Element)
                                this.setturnos(Convert.ToInt32(nuevo.ReadElementString()));
                            break;
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
